<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/23
 * Time: 15:04
 */

namespace App\Api\Admin\Controllers;
use App\Models\User;

class ThirdPartyController extends CommonController
{


    public function EditMemberMark()
    {
        $uid = IT('uid');
        $mobile = IT('mobile');
        $membership = IT('membership',true,'请输入会员状态');
        if ($uid){
          $user_id =  User::where(['id'=>$uid,'status'=>1])->first();
          if (!$user_id){
              return json_error('此用户不存在');
          }
            $where= ['status'=>1,'id'=>$uid];
        }
        if($mobile){
            $user_mobie =  User::where(['mobile'=>$mobile,'status'=>1])->first();
            if (!$user_mobie){
                return json_error('此手机号码不存在');
            }
            $where= ['status'=>1,'mobile'=>$mobile];
        }
        $member_edit = User::where($where)->update(['membership'=>$membership]);
        if ($member_edit){
            return json_success('ok');
        }else{
            return json_error('修改失败');
        }
    }
    /**
     * @return array
     * 推广会员详情
     */
    public function MembereDtails(){
        $id=IT('id',true,'请输入ID');
        $data= MemberShip::where('id','=',$id)->first(['id','name','mobile','sex','number','avatar','bank_number','admission_fee','created_at','term_validity','ascription','time_admission']);
        if (!$data){
            return json_success('会员不存在');
        }
        $affiliation = D('affiliation')->where('id',$data['ascription'])->value('locality_name');
        $data['ascription_id'] = (int)$data['ascription'];
        $data['ascription'] = !empty($affiliation) ? $affiliation : '';
        $data['created_at'] =  !empty($data['time_admission']) ? $data['time_admission'] :'';
        return json_success('ok',$data);
    }
    /**
     * @return array
     * 推广会员修改
     */

    public function EditMember(){
        $id = IT('id',true,'请输入会员ID');
        $check_user = MemberShip::where('id','=',$id)->first();
        $name = IT('name');
        $sex = IT('sex');
        $mobile = IT('mobile');
        $number = IT('number');
        $admission_fee = IT('admission_fee');
        $time_admission = IT('time_admission');
        $bank_number = IT('bank_number');
        $term_validity = IT('term_validity');
        $ascription = IT('ascription');
        if(!$check_user){
            return json_error('暂无此用户');
        }
        if($name && ($name!== $check_user->name)){
            $update_user['name'] = $name ;
        }
        if($sex && ($sex !== $check_user->sex)){
            $update_user['sex']=$sex;
        }
        if($mobile && ($mobile !== $check_user->mobile)){
            $check_mobile = MemberShip::where(['mobile' =>$mobile,'status'=>0])->first();
            if($check_mobile){
                return json_error('手机号码已存在');
            }
            User::where('mobile',$mobile)->update(['membership'=>1]);
            $update_user['mobile']=$mobile;
        }
        if($number && ($number!== $check_user->number)){
            $update_user['number']=$number;
        }
        if($admission_fee && ($admission_fee!== $check_user->admission_fee)){
            $update_user['admission_fee']=$admission_fee;
        }
        if( $time_admission && ($time_admission !== $check_user->time_admission) ){
            $update_user['time_admission']=$time_admission;
        }
        if( $bank_number && ($bank_number!== $check_user->bank_number)){
            $update_user['bank_number']=$bank_number;
        }
        if($term_validity && ($term_validity!== $check_user->term_validity)){
            $update_user['term_validity']=$term_validity;
        }
        if($ascription && ($ascription!== $check_user->ascription)){
            $update_user['ascription']=$ascription;
        }
        $update_user['updated_at'] = time2date();
        $success_data =MemberShip::where('id','=',$id)->update($update_user);
        if($success_data){
            $check_mobile = User::where(['mobile'=>$mobile, 'status'=>1])->first();
            if($check_mobile){
                MemberShip::where('mobile','=',$mobile)->update(['avatar' =>$check_mobile->avatar]);
            }
            (new Log)->add($this->user, '修改推广会员，id为-' . $id );
            return json_success('ok');
        }elseif(!$success_data){
            return json_error('请填写，你要修改的内容');
        }else{
            return json_error('修改失败');
        }
    }
    /**
     * 推广会员删除
     */
    public function DelMember(){
        $id = IT('id',true,'请输入ID');
        $member_data = MemberShip::where('id','=',$id)->first();
        if(!$member_data){
            return json_error('此会员不存在');
        }
        $del_date = MemberShip::where('id','=',$id)->update(['status'=>1]);
        if($del_date){
            //TODO 删除成功，修改users 表中的会员状态
            User::where('mobile','=',$member_data->mobile)->update(['membership'=> 0]);
            (new Log)->add($this->user, '删除了推广会员，id为-' .$id );
            return json_success('ok');
        }else{
            return json_error('删除失败');
        }
    }



}